#include "video.h"
#include <QBuffer>
#include <QTimer>

Video::Video(MqttHandler *mqtt, QObject *parent)
    :QObject(parent), m_mqtt(mqtt)
{
    m_requestTimer = new QTimer(this);
    connect(m_requestTimer, &QTimer::timeout, [this](){
        QMQTT::Message msg(0, "/video/request", "1");
        m_mqtt->publish(msg);
    });

    connect(m_mqtt, &MqttHandler::dataUpSignal, this, &Video::processVideoData);
}

void Video::StartStream()
{
    m_requestTimer->start(100);
}

void Video::StopStream()
{
    m_requestTimer->stop();
}

void Video::processVideoData(const QByteArray &data)
{
    QImage frame;
    if(frame.loadFromData(data, "JPEG")){
        emit frameReceived(frame);
    }
}
